// 路由守卫组件
import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';

const RouteGuard = ({ children, requireAuth }) => {
  console.log('/src/components/RouteGuard路由守卫===>', children, requireAuth);
  const location = useLocation();
  const token = sessionStorage.getItem('token');

  if (requireAuth && !token) {
    // 如果需要认证且没有 token，则重定向到登录页
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return children;
};

export default RouteGuard;